using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo.Business
{
    public abstract class HttpRepository<T> where T:class,IEntity
    {
        protected HttpRepository(string cacheKey)
        {
            CacheKey = cacheKey;
        }
    
        internal virtual List<T> All
        {
            get
            {
                if(HttpContext.Current.Cache[CacheKey] == null )
                {
                    HttpContext.Current.Cache[CacheKey] = new List<T>();
                }
                return HttpContext.Current.Cache[CacheKey] as List<T>;
            }
        }
        protected string CacheKey { get; set; }
        
        public T GetById(Guid id)
        {
            return All.FirstOrDefault(p => p.Id == id);
        }
        public IEnumerable<T> GetAll()
        {
            return All;
        }
        public void Save(T person)
        {
            var thePerson = GetById(person.Id);
            if(thePerson != null)
            {
                All.Remove(person);
            }
            All.Add(person);

        }
        
    }
}